
import service from '@/utils/request'

export async function validateToken() {
    const token = localStorage.getItem('token');

    if (!token) {
        console.log('未找到token，请先登录');
        return;
    }

    try {
        service.post('/api/system/user/token/validate', {params: {
            token: token
        }}).then(res => {
            if (res.code === 200) {
                localStorage.setItem('token', res.data);
            }
            else {
                localStorage.removeItem('token');
                localStorage.removeItem('screenName');
                localStorage.removeItem('avatar');
                localStorage.removeItem('roletype');
                this.$router.push('/login');
            }
        })
    } catch (error) {
        this.$message.error("系统错误！"); 
        console.error('请求错误:', error);
    }
}